'''
Created on Sep 7, 2012

@author: petrina
'''
from network_indicator import * 
import logging
import datetime

class Network_completeness(Network_indicator):
    '''
    Calculates the Completeness for every time interval (using aggregated fcd-data).
    
    The unit is percent because the unit of the link completeness is percent too.
    
    Formula for calculating the indicator: 
       c_nw =  sum(c_i_t * l_t)/L
            where:
                - c_i ... completeness of the link t on the day t
                - l_t  ... length of the link t
                - L ... total length 
    
    unit: index value
    '''


    def __init__(self, links):
        '''
        @param links: linkset for calculating the indicator
        '''
        
        Network_indicator.__init__(self, links)
        self._name = 'Network Completeness'
        self._mr_name =  'nw_completeness'
        self._unit = '%'
        #self.unit.__doc__ = 'index value'

        # timestamps of the indicator values
        self._times = None
        
        # titles for plotting and exporting
        self._title = self._name
        self._exp_title = self._mr_name
        
        logging.debug("Network_compeleteness: created new Compeleteness for a link-network")
        
        
    #----------------------public methods of the class-------------------------

    
    def calculate(self):
        '''
        calculate the Network-completeness
        '''
        
        logging.info("Network_completeness: staring to calculate the Compeleteness")

        values = [] 
        times = []
        
        min_time = self._links.begintime
        max_time = self._links.endtime
        intervals = self._links.intervals
        
        date = min_time
        
        while date <= max_time: 
            
            for i in range(intervals):
                sum = 0
                
                m = 24 * 60 * i/intervals
                h = m/60
                m -= h * 60
                time = datetime.datetime(date.year, date.month, date.day, h, m, 0) 
                                
                # for all links on that time and date
                for link in self._links:
                    rec = link.records[i].search_data(date) 
                    
                    if rec is not None:
                        sum += min(rec.coverage, 1) * link.length  
                val = sum * 100/self._links.length

                
                values.append(val)
                times.append(time)
            # go to the next day
            date += datetime.timedelta(days=1)
        
        self._values = values
        self._times = times
        
        logging.debug("Network_completeness : finished calculating the NW-Compeleteness")

        return self._values
    
    
    
    def plot(self, plotobject):
        '''
        plot indicator values and timestamps (period of time)
        '''
        
        self._plot_period(plotobject, minval = 120)


    def export2csv(self, csvwriter):
        '''
        export indicator value and timestamp
        '''
        
        logging.info("Indicator: starting to export the indicator to a csv-file")
        
        csvwriter.open_writer(self._exp_title +'.csv')
        csvwriter.write(['timestamp','value'])
        for i in range(len(self._values)):
            csvwriter.write([self._times[i], self._values[i]])
        csvwriter.close_writer()
        
        logging.debug("Indicator: finished exporting the indicator to a csv-file")